/**
 * 二叉搜索树
 */
public class BinarySearchTree {
    static class TreeNode {
        public TreeNode left;
        public TreeNode right;
        public int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;


    //插入
    public void insert(int key){
        if (root == null){
            root = new TreeNode(key);
            return;
        }

        //插入核心
        TreeNode parent = null;
        TreeNode cur = root;
        TreeNode newNode = new TreeNode(key);
        while(cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else{
                //在二叉搜索树中不能同时插入两个数据
                return;
            }
        }

        //判断父节点的值，进而判断该节点是否插入父节点的左和右的叶子节点
        if (parent.val > key){
            parent.left = newNode;
        }
        if (parent.val < key){
            parent.right = newNode;
        }
    }



    //搜索与查询

    /**
     * 时间复杂度：
     *      最好情况： O(logN)
     *      最坏情况： O(N)
     * @param key
     * @return
     */
    public TreeNode search(int key){
        if (root == null){
            return null;
        }
        TreeNode cur = root;
        while(cur != null){
            if (cur.val < key){
                cur = cur.right;
            }else if (cur.val == key){
                return cur;
            }else {
                cur = cur.left;
            }
        }
        return null;
    }


    //删除的核心操作
    public void remove(int key){
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            //查询指定删除节点
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else {
                removeNode(parent,cur);
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        //删除节点的左节点为空
        if (cur.left == null){
            if (cur == root){
                //删除节点为根节点，其该删除节点没有左节点
                root = cur.right;
            }else if (cur == parent.left){
                //删除节点为父节点的左节点，且该删除节点没有左节点
                parent.left = cur.right;
            }else {
                //删除节点为父节点的右节点，且该删除节点没有左节点
                parent.right = cur.right;
            }
        }else if (cur.right == null){//删除节点的右节点为空
            if (cur == root){
                //删除节点为根节点，其该删除节点没有右节点
                root = cur.left;
            }else if (cur == parent.left){
                //删除节点为父节点的左节点，且该删除节点没有右节点
                parent.left = cur.left;
            }else {
                //删除节点为父节点的右节点，且该删除节点没有右节点
                parent.right = cur.left;
            }
        }else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }

            cur.val = target.val;
            if (targetParent.left == target){
                targetParent.left = target.right;
            }else {
                //如果cur节点的右侧节点的左侧节点为空，这个时候cur的值已经被替换完成，那么我们直接用
                //右侧的节点连上，确保结构是准确的（其实就是把cur的右侧整体节点向上窜一位
                targetParent.right = target.right;
            }
        }
    }

    /**
     * 替换删除
     * 找到一个合适的数据将cur.val替换
     * 1.左子树找到最大值 --> 左树的最右边的节点（最大节点），[右子树是空的]
     * 2.右子树找到最小值 --> 右树的最左边的节点（最小节点），[左子树是空的]
     */
}
